// 我的战绩界面
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tixianHidden: true,
    isSaveHid: true,
    mineBgImg: "https://p021.zhenlutech.com/uploads/template/zhanji.jpg",
    //  mineBgImg:"/images/shine_performance.jpg"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    wx.getSystemInfo({

      success: function (res) {
        that.setData({
          height: res.windowHeight,
          width: res.windowWidth
        })
      },
    })
    wx.authorize({
      scope: 'scope.userInfo',
      success() {
        wx.getUserInfo({
          success: function (res) {

            var userInfo = res.userInfo
            that.setData({
              avatarUrl: userInfo.avatarUrl,

            });

            getApp().request("update_userinfo", res.userInfo, true);

          }
        })
      }, fail() {
        wx.openSetting({});
      }
    })
  },
  onShow: function () {

    var that = this;


    app.request("userInfo", {}, true).then(function (data) {

      var mineData = data.data;
      that.setData({
        inputMoney: mineData.user.inputMoney,
        earnMoney: mineData.user.earnMoney,
        signNum: mineData.user.signNum,
        sign: mineData.sign,
        money: mineData.user.money
      });
    });


    // 每日几点之前进入小金库
    var mShowHour = parseInt(app.globalData.endTime.split(":")[0]); // 时
    var mShowMinte = parseInt(app.globalData.endTime.split(":")[1]); // 分
    var mShowSecond = mShowHour * 3600 + mShowMinte * 60;
    // 公布结果的时间

    var sSecond = mShowSecond + 1800;
    var shour = parseInt(sSecond / 3600);
    var sminute = parseInt((sSecond % 3600) / 60);
    var resultTime
    if (sminute === 0) {
      resultTime = shour + ":" + "00";
    } else {
      resultTime = shour + ":" + sminute;
    }

    this.setData({
      resultTime: resultTime
    });
  },
  //提现点击事件
  withdrawOnClick: function (e) {
    this.setData({
      tixianHidden: false,
    });
  },

  //规则的点击事件
  withdrowRule: function (e) {
    wx.navigateTo({
      url: '/pages/withdraw-deposit/withdraw-deposit',
    })
  },


  // 晒战绩的点击事件
  // onShowMyPerformanceClick: function (event) {
  //   var that = this;
  //   console.log("晒成绩的点击事件你来点反应 ");

  //   app.request("poster", {}, true).then(function (data) {

  //     that.setData({
  //       shineImgUrl: data.date,
  //       isSaveHid: false,
  //     });
  //     console.log(that.data.shineImgUrl);

  //   })
  // },

  // 保存照片到相册的点击事件
  btnSaveImgClick: function (event) {
    var that = this;
    this.setData({
      isSaveHid: true
    });
    wx.downloadFile({
      url: that.data.shineImgUrl,
      success: function (res) {
        console.log("下载:" + res.tempFilePath);
        var tempFilePath = res.tempFilePath;

        wx.authorize({
          scope: "scope.writePhotosAlbum",
          success() {
            wx.showLoading({
              title: "保存中",
              mask: true
            })

            wx.saveImageToPhotosAlbum({
              filePath: tempFilePath,
              success(res) {
                wx.showModal({
                  title: "保存成功",
                  confirmColor: "#ff7f1a",
                  content: "战绩已保存至手机相册",
                  success: function () { },
                  fail: function () { }

                }),
                  wx.hideLoading();
              },
              fail: function (res) {
                wx.hideLoading();
                wx.showModal({
                  title: "保存失败",
                  content: "需要您的授权才能保存。您可以在小程序设置里授权。",
                  success: function () {
                    wx.openSetting({})
                  }
                })
              }
            })
          },
          fail: function (err) {
            console.log(err);
          }
        })
      }
    })
  },

  // 关闭保存图片的按钮
  closeSaveImgClick: function () {
    console.log("关闭保存");
    this.setData({
      isSaveHid: true
    });
  },

  // 意见反馈的点击事件
  opinionClick: function (event) {
    console.log("点击了意见反馈");
    wx.navigateTo({
      url: "../opinion/opinion",
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },



  // 分享的方法
  onShareAppMessage: function (res) {
    var that = this;
    return {
      title: "我已坚持早起" + that.data.signNum + "天，累计赚取" + that.data.earnMoney + "元。",
      path: "/pages/index/index",
      success: function (res) {
        // 转发成功
        shareTickets: ({

        })
        that.onShow();
      },

      fail: function (res) {
        // 转发失败
        that.onShow();
      }
    }
  },

  /*提现金额改变监听 */
  tixianChange: function (e) {
    console.log(e.detail.value);
    this.setData({ tixianMoney: e.detail.value });
  },

  // 提现灰色背景的点击事件
  tixianBgOnClick: function (e) {
    this.setData({
      tixianHidden: true,
    });
  },
  // 提现取消点击事件
  cancelBtnOnClick: function (e) {
    console.log("取消");
    this.setData({
      tixianHidden: true,
    });
  },
  // 提现确定的点击事件
  sureBtnOnClick: function (e) {
    console.log("确定");
    var that = this;
   
    if (this.data.tixianMoney <= this.data.money) {
      app.request("withdraw_money", { money: that.data.tixianMoney }, true, true).then(function (data) {
        if (data.Code == 0) {//谢谢您的参与，今天已被抢完！
          wx.showToast({
            title: "谢谢您的参与" + "\n" + "今天已被抢完!",
            image: '/images/icon_fail.png',
          })
        }
        if(data.Code==200){
          wx.showToast({
            title: "提现成功",
            icon:"success",
          
          })
          that.onShow();
        }
        if (data.Code == 302) {
          wx.navigateTo({
            url: "/pages/red-packet/red-packet?redUrl=" + data.Msg,
          })
        }
      });
      this.setData({
        tixianHidden: true,
      });
    } else {
      wx.showToast({
        title: '余额不足',
        image: '/images/icon_fail.png',
      })
    }
  },
})